package com.yiliao.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.yiliao.service.PrivatePhotoService;
import com.yiliao.util.DateUtils;
import com.yiliao.util.MessageUtil;
import com.yiliao.util.cache.CacheRemove;

/**
 * 私密照片服务实现
 * 
 * @author Administrator
 * 
 */
@Service("privatePhotoService")
public class PrivatePhotoServiceImpl extends ICommServiceImpl implements
		PrivatePhotoService {

	private MessageUtil mu = null;

	/*
	 * 获取私密相册列表(non-Javadoc)
	 * 
	 * @see com.yiliao.service.PrivatePhotoService#getPrivatePhotoList(int)
	 */
	@Override
	public JSONObject getPrivatePhotoList(int page,int search_name,int fileType) {
		JSONObject json = new JSONObject();
		try {
			
			String cSql = "SELECT COUNT(d.t_id) AS total FROM t_album d LEFT JOIN t_user u ON d.t_user_id = u.t_id WHERE  t_is_del = 0 ";
			
			if(search_name != -1){
				cSql = cSql + " AND d.t_auditing_type = "+ search_name ;
			}
			
			if(fileType >=0 && fileType <=1) {
				cSql = cSql + " AND t_file_type = " + fileType;
			}
			
			Map<String, Object> totalMap = this.getFinalDao().getIEntitySQLDAO().findBySQLUniqueResultToMap(cSql);
			
			
			String qSql = "SELECT d.t_id,d.t_user_id,d.t_title,d.t_video_img,d.t_addres_url,d.t_file_type,d.t_money,d.t_auditing_type,d.t_is_private,u.t_nickName,u.t_idcard FROM t_album d LEFT JOIN t_user u ON d.t_user_id = u.t_id WHERE  t_is_del = 0 ";
			
			if(search_name != -1){
				qSql = qSql + " AND d.t_auditing_type = "+ search_name ;
			}
			
			if(fileType >=0 && fileType <=1) {
				qSql = qSql + " AND t_file_type = " + fileType;
			}
			
			qSql = qSql + " ORDER BY d.t_id DESC LIMIT ?,10";
			
			List<Map<String, Object>> dataList = this.getFinalDao().getIEntitySQLDAO().findBySQLTOMap(qSql, (page-1)*10);
			
			
			json.put("total", totalMap.get("total"));
			json.put("rows", dataList);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取私密相册列表异常!", e);
		}
		return json;
	}

	/*
	 * 点击禁用(non-Javadoc)
	 * @see com.yiliao.service.PrivatePhotoService#clickSetUpEisable(int)
	 */
	@Override
	public MessageUtil clickSetUpEisable(int t_id,int userId) {
		try {
			
			String sql = "DELETE FROM t_album WHERE t_id = ?";
			
			this.getFinalDao().getIEntitySQLDAO().executeSQL(sql, t_id);
			
			sql="select t_user_id from  t_album  where t_user_id =? and t_file_type=1 and t_is_del=0 and t_auditing_type=1 and t_is_private=0";
			
			List<Map<String,Object>> returnList = this.returnList(sql, userId);
			if(returnList.isEmpty()) {
				this.getFinalDao().getIEntitySQLDAO().executeSQL("UPDATE t_home_table SET t_is_public =0 WHERE t_id = ? ", userId);
			}
			//存储消息
			String messageSql = "INSERT INTO t_message (t_user_id, t_message_content, t_create_time, t_is_see) VALUES (?, ?, ?, ?);";
			this.getFinalDao().getIEntitySQLDAO().executeSQL(messageSql, userId,
					"您提交的资料不符合平台规定，请您仔细阅读相关规定再重新提交。",
					DateUtils.format(new Date(), DateUtils.FullDatePattern),0);
			
			mu = new MessageUtil(1, "操作成功!");
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("点击禁用图片异常!", e);
			mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

	/**
	 * 点击通过审核
	 */
	@Override
	public MessageUtil onclickHasVerified(int t_id) {
		try {
			
			String sql = "UPDATE t_album SET t_auditing_type = 1 WHERE t_id=?";
			
			this.getFinalDao().getIEntitySQLDAO().executeSQL(sql, t_id);
			
			//判断当前数据是否是公开视频
			 
			Map<String, Object> album = this.getFinalDao().getIEntitySQLDAO().findBySQLUniqueResultToMap(
			"SELECT *  FROM t_album WHERE t_id = ? ",t_id);
			
			//更新用户主页表中的公共视频为1
			if(album.get("t_file_type").toString().equals("1") && album.get("t_is_private").toString().equals("0")
					&& album.get("t_is_del").toString().equals("0") && album.get("t_auditing_type").toString().equals("1")) {
				this.getFinalDao().getIEntitySQLDAO().executeSQL("UPDATE t_home_table SET t_is_public = 1 WHERE t_id = ? ", album.get("t_user_id"));
			}
			
			mu = new MessageUtil(1, "操作成功!");
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("点击通过审核异常!", e);
			mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

	@Override
	public MessageUtil setFirsetPhoto(int userId, int albumId) {
		try {
			this.executeSql("update t_album set t_is_first =0 where t_user_id=? and t_file_type=1  ", userId);
			this.executeSql("update t_album set t_is_first =1 where t_id=? and t_file_type=1 and t_auditing_type=1 ", albumId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new MessageUtil(1, "修改成功!");
	}
	
	
	
}
